package com.gopay.bis.common.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

public class DocumentUtils {
	/**
	 * ticketId xpath路径
	 */
	public static final String XPATH_TICKETID = "//body/ticketId";
	public static final String XPATH_URI = "//body/uri";
	public static final String XPATH_HDLSTS = "//header/hdlSts";
	public static final String XPATH_RTNCD = "//header/rtnCd";
    /**
     * 将文本转换成Document对象
     * @param xmlData
     * @return
     */
    public static Document getDocument(String xmlData) {
        Document document = null;

        try {
            document = DocumentHelper.parseText(xmlData);
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return document;
    }
    
    /**
     * muye
     * 利用Xpath和Dom直接定位元素
     */
    public static String getTagText(Document doc ,String xPath){
    	Node node = null;
    	String val = null;
    	try{
    		node = doc.selectSingleNode(xPath);
    		val = node.getText();
    	}catch(Exception e){
    		return "xPath is not valiable or fail " +xPath;
    	}
    	return val;
    }
}
